/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.util;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.HasValue;
import redora.client.Persistable;

/**
 * The [Pojo]Util classes move data back and forth from pojos to widgets to json.
 * These classes extend ClientUtilBase for some shared functionality.
 *
 * @see ClientUtil
 * @author Redora (www.redora.net)
 */
public abstract class ClientUtilBase<T extends Persistable> implements ClientUtil<T> {

    /**
     * @see HasValue
     * @param widget (Mandatory) Widget implementing HasValue
     * @return True if the widget's getValue is not null or empty
     */
    public static boolean isNull(HasValue widget) {
        return widget.getValue() == null || widget.getValue().toString().trim().length() == 0;
    }

    public static Persistable.Scope scope(JSONObject obj) {
         return Persistable.Scope.values()[Integer.parseInt(obj.get("_scope").toString())];
    }

    public static String chain(String[] info) {
        String retVal = "";
        boolean beginning = true;
        for (String addMe : info) {
            if (addMe != null) {
                if (beginning)
                    beginning = false;
                else
                    retVal += " ";
                retVal += addMe;
            }
        }
        return retVal;
    }
}

